public class SellActivity extends Thread {
	   private int target;
	   private int limit;
	   private String sStatus = "";
	   private PennerInfo info;
	   private PennerStats stats;
	   private Connection connection;

	   public SellActivity(Connection pennerConn, PennerInfo pennerInfo, PennerStats pennerStats) {  
		   info = pennerInfo;
		   stats = pennerStats;
		   connection = pennerConn;
	   }

	   public void setTargetShare(int newTarget) {  
		   target = newTarget;
	   }

	   public void setLimit(int newLimit) {  
		   limit = newLimit;
	   }

	   public int getCurrentCash() {  
		   return (int) (info.cash*100);
	   }

	   public int getCurrentShare() {  
		   return info.crapShare;
	   }

	   public int getCurrentChk() {  
		   return info.crapChk;
	   }

	   public int getCurrentCraps() {  
		   return info.craps;
	   }

	   public String getStatusLine() {
		   return sStatus;
	   }
		   
	   public void run() {  
		   int share=0;
		   int sellCount=0;
		   int sellMaxCount=0;
		   int chkval=0;
		   while (!interrupted()) {
			   if ((getCurrentCash()+getCurrentShare()) > limit) {
				   sStatus = "max. Kontostand erreicht!";
				   try {
					SellActivity.sleep(60000);
				   } catch (InterruptedException e) {
			        	stats.setStatus("Verkaufschlaf Fehler");
				   }
			   } else if ((getCurrentShare() >= target) & (getCurrentCraps() > 0)) {
				   sStatus = "verkaufe...";
				   share = getCurrentShare();
				   chkval = getCurrentChk();
				   sellMaxCount = getCurrentCraps();
				   sellCount = (int) Math.floor((limit-getCurrentCash())/share);
//				   System.out.println("max Count: "+String.valueOf(sellMaxCount));
//				   System.out.println("Count: "+String.valueOf(sellCount));
				   try {
					   if ((sellMaxCount-sellCount) > 0) {
						   connection.startSelling(sellCount, chkval, sellMaxCount);
						   if (connection.sellSuccess) {
							   stats.sellBottles(sellCount, share);
							   sStatus = "Erfolgreich verkauft!";
							   info.getInfo(connection);
						   } else {
							   sStatus = "Verkauf fehlgeschlagen!";
						   }
					   } else {
						   connection.startSelling(sellMaxCount, chkval, sellMaxCount);
						   if (connection.sellSuccess) {
							   stats.sellBottles(sellMaxCount, share);
							   sStatus = "Erfolgreich verkauft!";
							   info.getInfo(connection);
						   } else {
							   sStatus = "Verkauf fehlgeschlagen!";
						   }
					   }
					   SellActivity.sleep(10000);
				   }
				   catch(Exception e)
				   {  
			        	stats.setStatus("Verkauf Fehler");
				   }
			   } else if (getCurrentCraps() == 0) {
				   sStatus = "Keine Flaschen!";
				   try { 
					   SellActivity.sleep(10000);
				   }
				   catch(InterruptedException e)
				   {  
			        	stats.setStatus("keine FLaschen Fehler");
				   }
			   } else {
				   sStatus = "Kurs zu niedrig!";
				   try { 
					   SellActivity.sleep(10000);
				   }
				   catch(InterruptedException e)
				   {  
			        	stats.setStatus("niedriger Kurs Fehler");
				   }
			   }
	      }
	   }

}
